package com.lry.netty01.server.processor;

import com.lry.netty01.server.entity.FixedFrame;
import com.lry.netty01.server.entity.Packet;
import com.lry.netty01.server.utils.ChannelUtil;
import com.lry.netty01.server.utils.CommandUtil;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.Queue;

@Slf4j
public class WarningRequestProcessor implements RequestProcessor {

    private RequestProcessor next;

    public WarningRequestProcessor(RequestProcessor next) {
        this.next = next;
    }

    @Override
    public boolean processRequest(Channel channel, FixedFrame fixedFrame) {
        if(fixedFrame.getCmd()== CommandUtil.WARNING_CMD) {
            short devId = fixedFrame.getDevId();
            byte degree = fixedFrame.getData();
            log.info(devId+"的温度为"+degree+"度");

            ChannelUtil.devId_2_degree.put(devId,degree);
            //唤醒packet，返回给web调用方
            Queue<Packet> pendingQueue = ChannelUtil.PENDING_MAP.get(fixedFrame.getDevId());
            if(null!=pendingQueue&&!pendingQueue.isEmpty()){
                Packet packet = pendingQueue.poll();
                synchronized (packet){
                    packet.response = fixedFrame;
                    packet.finished = true;
                    packet.notifyAll();
                }
            }
            return true;
        }
        return false;
    }


}
